// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Constraint/GRDSETParser.h"

#include "DataStructure/Input/Constraint/GRDSETData.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (GRDSET);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
using namespace SG::DataStructure::FEM;

SG_IMPORT_BDF_NAMESPACE_OPEN

extern SG::DataStructure::FEM::GRDSETData* g_grdset;
SG_IMPORT_BDF_NAMESPACE_CLOSE

enum GRDSETKey : std::uint8_t
{
    GRDSETKey_CP   = 3,
    GRDSETKey_CD   = 7,
    GRDSETKey_PS   = 8,
    GRDSETKey_SEID = 9
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseGRDSET (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // Delete the previous data
    if (g_grdset != nullptr)
        delete g_grdset;

    g_grdset = new GRDSETData ();

    g_grdset->m_domainId = 1;

    // 初始化当前处理的字符串条目的ID
    int curIDKey = 1;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            // 如果当前条目为空，跳过当前循环并处理下一个条目
            curIDKey++;
            continue;
        }

        // 根据条目ID，将解析后的数值存入对应的GRAV字段
        switch (curIDKey)
        {
        case GRDSETKey_CP:
            g_grdset->m_CP = convertToI64 (curStrKey);
            break;
        case GRDSETKey_CD:
            g_grdset->m_CD = convertToI64 (curStrKey);
            break;
        case GRDSETKey_PS:
            g_grdset->m_PS = convertToI64 (curStrKey);
            break;
        case GRDSETKey_SEID:
            g_grdset->m_SEID = convertToI64 (curStrKey);
            break;
        default:
            // 如果条目ID不匹配，跳过处理
            break;
        }

        // 增加条目ID，以便处理下一个条目
        curIDKey++;
    }

    return Common::Status::STATUS_Success;
}